package com.googlecode.classgrep.visitor.annotated;

import java.util.ArrayList;
import java.util.List;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.commons.EmptyVisitor;

import com.googlecode.classgrep.GrepRobotInternal;
import com.googlecode.classgrep.grep.annotated.AnnotationGrep;
import com.googlecode.classgrep.info.AnnotationInfo;

public class AnnotationDefVisitor extends CommonDef implements AnnotationVisitor {

	private AnnotationInfo info;
	
	private AnnotationGrep annotationGrep;
	
	public AnnotationDefVisitor(GrepRobotInternal grepClient, AnnotationInfo info) {
		super(grepClient);
		this.info = info;
		this.annotationGrep = grepClient.getAnnotationGrep();
	}

	public void visit(String name, Object value) {
	}

	public void visitEnum(String name, String desc, String value) {
	}

	public AnnotationVisitor visitAnnotation(String name, String desc) {
		AnnotationInfo annotationInfo = new AnnotationInfo(desc);
		if(grepClient.getAnnotationCriterion().contains(annotationInfo.getName())){
	        info.getAnnotations().add(annotationInfo);
	        List<AnnotationInfo> infos = annotationGrep.getAnnotatedAnnotationMap().get(annotationInfo.getName());
	        if (infos == null) {
	            infos = new ArrayList<AnnotationInfo>();
	            annotationGrep.getAnnotatedAnnotationMap().put(annotationInfo.getName(), infos);
	        }
	        infos.add(info);
	        return new AnnotationDefVisitor(grepClient, annotationInfo);
		}else{
			return new EmptyVisitor();
		}
	}

	public AnnotationVisitor visitArray(String name) {
		return null;
	}

	public void visitEnd() {

	}

}
